package com.neu.questionnaireplus.controller;

import com.neu.questionnaireplus.entity.Answerer;
import com.neu.questionnaireplus.entity.Group;
import com.neu.questionnaireplus.entity.HttpResponseEntity;
import com.neu.questionnaireplus.service.GroupService;
import com.neu.questionnaireplus.utils.common.CommonUtils;
import com.neu.questionnaireplus.utils.constant.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Description: 使用swagger进行测试, url未使用rap2的，项目整合时再做修改
 * @Param:
 * @return:
 * @Author: ~FHX~ (:TSunS:)
 * @Date: 2022/10/30
 */
@RestController
@RequestMapping("/group")
@CrossOrigin
@Api("群组服务")
@Validated
public class GroupController {
    @Autowired
    private GroupService groupService;

    @ApiOperation("查询群组下的答者")
    @RequestMapping(value = "/queryAnswerers", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity queryAnswerers(@RequestBody Map<String,Object> map){
        HttpResponseEntity responseEntity = new HttpResponseEntity();
        try {
            String id = map.get("id").toString();
            List<Answerer> answerers = groupService.queryAnswererByGroup(id);
            responseEntity
                .setCode(HttpStatus.SUCCESS)
                .setData(answerers)
                .setMessage("success");
        }catch (Exception e){
            responseEntity
                .setCode(HttpStatus.ERROR)
                .setData(null)
                .setMessage(e.getMessage());
        }finally {
            return responseEntity;
        }
    }

    @ApiOperation("在群组中移除答者")
    @RequestMapping(value = "/removeAnswerer", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity removeAnswerer(@RequestBody Map<String,Object> map){
        HttpResponseEntity responseEntity = new HttpResponseEntity();
        try {
            String groupId = map.get("groupId").toString();
            String answererId = map.get("answererId").toString();
            groupService.deleteAnswererFromGroup(groupId, answererId);
            responseEntity
                .setCode(HttpStatus.SUCCESS)
                .setData(null)
                .setMessage("sucess");
        }catch (Exception e){
            responseEntity
                .setCode(HttpStatus.ERROR)
                .setData(null)
                .setMessage(e.getMessage());
        }finally {
            return responseEntity;
        }
    }

    @ApiOperation("用户创建群组")
    @RequestMapping(value = "/createGroup", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity createGroup(@RequestBody Map<String,Object> map){
        HttpResponseEntity responseEntity = new HttpResponseEntity();
        try {
            String groupname = map.get("name").toString();
            Group group = groupService.queryGroupByGroupname(groupname);
            if (group == null){
                System.err.println(group);
                String introduction = map.get("introduction").toString();
                group = new Group();
                group
                    .setGroupname(groupname)
                    .setIntroduction(introduction)
                    .setCreatedDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()))
                    .setDelFlag(0)
                    .setNumbers(0)
                    .setId(CommonUtils.getUUID());
                boolean b = groupService.insertGroup(group);
                if (b){
                    responseEntity
                        .setCode(HttpStatus.SUCCESS)
                        .setData(group)
                        .setMessage("success");
                }else {
                    responseEntity
                        .setCode(HttpStatus.ERROR)
                        .setData(null)
                        .setMessage("新增失败");
                }
            }else {
                responseEntity
                    .setCode(HttpStatus.ERROR)
                    .setData(null)
                    .setMessage("群组已经存在");
            }
        }catch (Exception e){
            responseEntity
                .setCode(HttpStatus.ERROR)
                .setData(null)
                .setMessage(e.getMessage());
        }finally {
            return responseEntity;
        }
    }


    @ApiOperation("修改群组信息")
    @RequestMapping(value = "/updateGroup", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity updateGroup(@RequestBody Map<String,Object> map){
        HttpResponseEntity responseEntity = new HttpResponseEntity();
        try {
            String id = map.get("id").toString();
            Group group = groupService.queryGroupByID(id);
            if (group != null){
                String groupname = map.get("groupname").toString();
                String introduction = map.get("introduction").toString();
                group
                        .setGroupname(groupname)
                        .setIntroduction(introduction);
                boolean b = groupService.updateGroup(group);
                if (b){
                    responseEntity
                            .setCode(HttpStatus.SUCCESS)
                            .setData(groupname)
                            .setMessage("修改成功");
                }else {
                    responseEntity
                            .setCode(HttpStatus.ERROR)
                            .setData(null)
                            .setMessage("修改失败");
                }
            }else {
                responseEntity
                        .setCode(HttpStatus.ERROR)
                        .setData(null)
                        .setMessage("用户不存在");
            }
        }catch (Exception e){
            responseEntity
                    .setCode(HttpStatus.ERROR)
                    .setData(null)
                    .setMessage(e.getMessage());
        }finally {
            return responseEntity;
        }
    }

    @ApiOperation("删除群组（伪删除）")
    @RequestMapping(value = "/deleteGroup", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity deleteGroup(@RequestBody Map<String,Object> map){
        HttpResponseEntity responseEntity = new HttpResponseEntity();
        try {
            String id = map.get("id").toString();
            Group group = groupService.queryGroupByID(id);
            //System.err.println(group);
            if (group != null){
                boolean b = groupService.deleteGroupInfo(group);
                if (b){
                    responseEntity
                            .setCode(HttpStatus.SUCCESS)
                            .setData(group)
                            .setMessage("success");
                }else {
                    responseEntity
                            .setCode(HttpStatus.ERROR)
                            .setData(null)
                            .setMessage("群组删除失败");
                }
            }else {
                responseEntity
                        .setCode(HttpStatus.ERROR)
                        .setData(null)
                        .setMessage("群组不存在");
            }
        }catch (Exception e){
            responseEntity
                    .setCode(HttpStatus.ERROR)
                    .setData(null)
                    .setMessage(e.getMessage());
        }finally {
            return responseEntity;
        }
    }


    @ApiOperation("展示所有未被删除的群组")
    @RequestMapping(value = "/showGroup", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity showAllGroups() {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            List<Group> groups = groupService.queryExistGroup();
            httpResponseEntity
                    .setCode(HttpStatus.SUCCESS)
                    .setData(groups)
                    .setMessage("success");
        } catch (Exception e) {
            httpResponseEntity
                    .setCode(HttpStatus.ERROR)
                    .setData(null)
                    .setMessage(e.getMessage());
        } finally {
            return httpResponseEntity;
        }
    }

    @ApiOperation("模糊查询所有未被删除的群组")
    @RequestMapping(value = "/showQueryGroup", method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity showQueryGroups(@RequestBody Map<String,Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            String queryStr = map.get("keyword").toString();
            List<Group> groups = groupService.queryGroupByName(queryStr);
            httpResponseEntity
                    .setCode(HttpStatus.SUCCESS)
                    .setData(groups)
                    .setMessage("success");
        } catch (Exception e) {
            httpResponseEntity
                    .setCode(HttpStatus.ERROR)
                    .setData(null)
                    .setMessage(e.getMessage());
        } finally {
            return httpResponseEntity;
        }
    }
}
